package com.bootdo.collection.controller;

import com.bootdo.collection.domain.PushStationDO;
import com.bootdo.collection.service.PushStationService;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;


/**
 * 推送站点管理
 */
@RequestMapping("/collection/pushStation")
@Controller
public class PushStationController {
    String prefix = "collection/station/push";
    @Autowired
    private PushStationService pushStationService;

    /**
     *查询推送站点
     * @param params offset:起始位置  limit:查询的条数
     * @return
     */
    @GetMapping("/list")
    @ResponseBody()
    public PageUtils list(Map<String,Object> params){
        List<PushStationDO> stationList = pushStationService.list(params);
        int total = pushStationService.getTotal();
        PageUtils pageUtils = new PageUtils(stationList, total);
        return pageUtils;
    }

    /**
     *图集中查询所有的上传站点
     * @return
     */
    @GetMapping("/getPushStation")
    @ResponseBody()
    public List<PushStationDO> getPushStation(){
        List<PushStationDO> stationList = pushStationService.list(null);
        return stationList;
    }


    @GetMapping("/")
    String pushStation() {
        return prefix + "/push_station";
    }

    /**
     *
     * @param id 站点id
     * @param model 通过model返回信息
     * @return
     */
    @Log("编辑站点")
    @GetMapping("/edit/{id}")
    String edit(@PathVariable("id") int id, Model model) {
        PushStationDO pushStationDO = pushStationService.get(id);
        model.addAttribute("pushStation",pushStationDO);
        return prefix + "/edit";
    }

    /**
     * 修改站点信息,如站点的ftp等
     */
    @ResponseBody
    @PostMapping("/update")
    public R update(PushStationDO sta) {
        if (pushStationService.update(sta) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除站点
     * @param pushStationId
     * @return
     */
    @ResponseBody
    @PostMapping("/remove")
    public R remove(Integer pushStationId){
        if(pushStationService.remove(pushStationId) > 0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除
     * @param pushStationIds
     * @return
     */
    @ResponseBody
    @PostMapping("/batchRemove")
    public R batchRemove(@RequestParam("pushStationIds[]")Integer[] pushStationIds){
        if(pushStationService.batchRemove(pushStationIds) > 0){
            return R.ok();
        }
        return R.error();
    }

    @Log("添加站点")
    @GetMapping("/add")
    String add() {
        return prefix + "/add";
    }

    @PostMapping("/save")
    @ResponseBody()
    public R save(PushStationDO pushStationDO){
        if(pushStationService.save(pushStationDO) > 0){
            return R.ok();
        }
        return R.error();
    }
    @Log("下载日志")
    @RequestMapping("/downApplog")
    @ResponseBody
    public void downLoadLog(HttpServletResponse response){
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");
            String file = time.format(System.currentTimeMillis());
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition","attachmentl;fileName=applog.log");
            String separator = File.separator;
            FileInputStream fileInputStream = new FileInputStream(System.getProperty("user.dir") + separator+"applog" +separator+ file + separator + file +".log");
            OutputStream outputStream = response.getOutputStream();
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1){
                outputStream.write(bytes);
            }
            outputStream.close();
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
